<?php
namespace app\common\job;

use think\Db;
use think\queue\Job;

class Shop
{

    /**
     * 店铺信息导入处理
     */
    public function importShop(Job $job, $data)
    {
        Db::startTrans();
        try {
            \think\Log::write('queue-' . get_class() . '-importShop' . '：开始执行');
//            $jsonData = json_encode($data);
//            \think\Log::write("具体执行任务接受到的参数：{$jsonData}");

            if(empty($data['state']) || empty($data['shopname']) || empty($data['name']) || empty($data['number']) || empty($data['mobile']) || empty($data['image']) || empty($data['wechat'])) {
                // 删除 job
                $job->delete();
                throw new \Exception('必填数据不完整:'. json_encode($data));
            }

            //添加用户
            $user = model('app\common\model\User')->get(['mobile' => $data['mobile']]);
            if(!$user){
                $Auth = new \app\common\library\Auth;
                $Auth->register($data['mobile'],'123456','',$data['mobile'],['nickname' => $data['shopname']]);
                $user_id = $Auth->id;
            }else{
                $user_id = $user->id;
            }

            //添加店铺认证表数据
            $insert_auth_array = [
                'user_id' => $user_id,
                'state' => $data['state'],
                'shopname' => $data['shopname'],
                'name' => $data['name'],
                'number' => $data['number'],
                'mobile' => $data['mobile'],
                'image' => $data['image'],
                'trademark' => $data['trademark'],
                'wechat' => $data['wechat'],
                'avatar' => $data['avatar'],
                'bio' => $data['bio'],
                'city' => $data['city'],
                'certificate' => $data['certificate'],
                'verify' => 2,
            ];
            (new \app\admin\model\wanlshop\Auth)->create($insert_auth_array);

            Db::commit();

            \think\Log::write('queue-' . get_class() . '-importShop' . '：执行成功');
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            Db::rollback();
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-importShop' . '：执行失败，错误信息：' . $e->getMessage() . ';Line: ' . $e->getLine());
        }
    }

}